/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FriendList.java
 *
 * Created on 2011-6-22, 16:33:43
 */

package com.musictalk.view;

import com.musictalk.message.model.*;
import com.musictalk.model.business.ManageChat;
import com.musictalk.model.business.ManageChatRoom;
import com.musictalk.model.business.ManageClientConnThread;
import com.musictalk.model.business.ManageFriendList;
import com.musictalk.model.connect.OnlineFriendThread;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author AIDEN
 */
public class FriendList extends javax.swing.JFrame {

    private LoginUser lUser;
    private OnlineFriendThread onlineThread;
    /** Creates new form FriendList */
    public FriendList(LoginUser lu) {
        this.lUser = lu;
        friendMap = new HashMap<JLabel,User>();
        this.setTitle(lUser.getLoginUser().toString());

        ManageFriendList.addFriendList(lUser.getLoginUser().getUID(), this);
        onlineThread = new OnlineFriendThread(lu.getLoginUser().getUID());
        onlineThread.start();

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listPanel = new javax.swing.JPanel();
        for (int i=0; i < this.lUser.getFriends().size(); i++) {
            JLabel aF = new JLabel(lUser.getFriends().get(i).toString());
            aF.setEnabled(false);
            User temp = lUser.getFriends().get(i);
            aF.addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount()==2) {
                        selectFriend(e);
                    }
                }

                public void mousePressed(MouseEvent e) {

                }

                public void mouseReleased(MouseEvent e) {

                }

                public void mouseEntered(MouseEvent e) {

                }

                public void mouseExited(MouseEvent e) {

                }
            }); 
            this.friendMap.put(aF, lUser.getFriends().get(i));
            listPanel.add(aF);
        }
        jPanel1 = new javax.swing.JPanel();
        roomButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        room = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        listPanel.setBackground(new java.awt.Color(255, 255, 255));
        listPanel.setLayout(new java.awt.GridLayout(50, 1, 4, 4));
        jScrollPane1.setViewportView(listPanel);

        roomButton.setText("聊天大厅");
        roomButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                roomButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(97, Short.MAX_VALUE)
                .addComponent(roomButton)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(roomButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("CoCo");

        room.setText("聊天大厅");
        room.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                roomActionPerformed(evt);
            }
        });
        jMenu1.add(room);

        jMenuItem1.setText("CoCo登出");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutAction(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void roomButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_roomButtonActionPerformed
        ChatRoom chatRoom = new ChatRoom(lUser.getLoginUser());
        chatRoom.setVisible(true);
        ManageChatRoom.addChatRoom(lUser.getLoginUser().getUID(), chatRoom);
    }//GEN-LAST:event_roomButtonActionPerformed

    private void logoutAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutAction
        Logout logout = new Logout(lUser.getLoginUser().getUID());
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ManageClientConnThread.getClientConnThread(lUser.getLoginUser().getUID()).gettSocket().getOutputStream());
            oos.writeObject(logout);
            ManageClientConnThread.getClientConnThread(lUser.getLoginUser().getUID()).gettSocket().close();
            oos.close();
            System.exit(0);
        } catch (IOException ex) {
            Logger.getLogger(FriendList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_logoutAction

    private void roomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_roomActionPerformed
        ChatRoom chatRoom = new ChatRoom(lUser.getLoginUser());
        chatRoom.setVisible(true);
        ManageChatRoom.addChatRoom(lUser.getLoginUser().getUID(), chatRoom);
    }//GEN-LAST:event_roomActionPerformed

    public void updateOnlineFriend(OnlineFriend onlineMsg){
        Iterator it = friendMap.keySet().iterator();
        while (it.hasNext()) {
            JLabel temp = (JLabel) it.next();
            temp.setEnabled(false);
            if (onlineMsg.getOnlineFriend().indexOf(friendMap.get(temp).getUID())!=-1) {
                temp.setEnabled(true);
            }
        }
    }

    private void selectFriend(MouseEvent e){
        JLabel friLabel = (JLabel)e.getSource();
        User friend = friendMap.get(friLabel);
        ChatFrame chat = new ChatFrame(lUser.getLoginUser(),friend);
        chat.setVisible(true);
        ManageChat.addChat(lUser.getLoginUser().getUID()+":"+friend.getUID(), chat);
    }

    private HashMap<JLabel,User> friendMap;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel listPanel;
    private javax.swing.JMenuItem room;
    private javax.swing.JButton roomButton;
    // End of variables declaration//GEN-END:variables

}
